package com.cwl.common.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cwl.common.constant.ApiConstant;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回对象
 *
 * @author cwivan
 * @version 1.0
 * @date 2018/9/10
 */
@Slf4j
@Data
@NoArgsConstructor
public class UEResult extends HashMap<String,Object> implements Serializable{

	private String code;
	private String msg;
	private JSONObject data = new JSONObject();
	private JSONObject originalResp = new JSONObject();

	private UEResult(String code, String msg, JSONObject data, Object extraData){
		this.code = code;
		this.msg = msg;
		this.data = data;

		this.put("code", this.code);
		this.put("msg", this.msg);
		this.put("data", this.data);

		if(extraData != null){
			this.put("data", extraData);
		}
	}

	private UEResult(String code, String msg, JSONObject data, Object extraData, JSONObject originalResp){
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.originalResp = originalResp;
		this.put("code", code);
		this.put("msg", msg);
		this.put("data", data);
		this.put("originalResp",originalResp);
		if(extraData != null){
			this.put("data", extraData);
		}

	}

	public String getString(String key){
		return data.getString(key);
	}


	/**
	 * for json
	 * @return
	 */
    public String getResult_code() {
        return code;
    }

    public String getResult_msg() {
        return msg;
    }

	@Override
	public String toString(){
		return "UEResult(code = " + this.code + ", msg = "+this.msg+", data = "+ JSON.toJSONString(this.data,SerializerFeature.WriteMapNullValue)+", originalResp = "+JSON.toJSONString(this.originalResp)+")";
	}

	public boolean isSuccess(){
		return ApiConstant.CODE_SUCCESS.equals(code);
	}

	public boolean isFail(){
		return !ApiConstant.CODE_SUCCESS.equals(code);
	}

	public static UEResultBuilder create() {
		return new UEResultBuilder();
	}

	public static class UEResultBuilder {
		private String code;
		private String msg;
		private JSONObject data = new JSONObject();
		private JSONObject originalResp = new JSONObject();

		private Object extraData;

		UEResultBuilder(){
		}

		public UEResultBuilder code(String code){
			this.code = code;
			return this;
		}

		public UEResultBuilder msg(String msg){
			this.msg = msg;
			return this;
		}

		public UEResultBuilder data(JSONObject data){
			this.data = data;
			return this;
		}

		public UEResultBuilder originalResp(JSONObject originalResp){
			this.originalResp = originalResp;
			return this;
		}

		public UEResultBuilder add(String key, Object content){
			this.data.put(key,content);
			return this;
		}

		public UEResultBuilder addAll(Map data){
			this.data.putAll(data);
			return this;
		}

		public UEResultBuilder success(){
			this.code = ApiConstant.CODE_SUCCESS;
			this.msg = ApiConstant.CODE_MSG_SUCCESS;
			return this;
		}

		public UEResultBuilder fail(){
			this.code = ApiConstant.CODE_FAIL;
			return this;
		}

		public UEResultBuilder fail(String msg){
			this.code = ApiConstant.CODE_FAIL;
			this.msg = msg;
			return this;
		}

		public UEResultBuilder fail(String code, String msg){
			this.code = code;
			this.msg = msg;
			return this;
		}

		public UEResultBuilder extra(Object extraData){
			this.extraData = extraData;
			return this;
		}

		public UEResult build(){
			return new UEResult(code,msg,data,extraData,originalResp);
		}

		@Override
		public String toString(){
			return "UEResultBuilder(code = " + this.code + ", msg = "+this.msg+", data = "+ data+")";
		}

	}

	public static UEResult error(){
		return error(ApiConstant.CODE_FAIL,"系统异常",null);
	}

	public static UEResult error(String msg){
		return error(ApiConstant.CODE_FAIL,msg,new JSONObject());
	}

	public static UEResult error(String code, String msg){
		return error(code,msg,null);
	}

	public static UEResult error(String code, String msg, JSONObject data){
		return UEResult.create().fail()
				.code(code)
				.msg(msg)
				.data(data)
				.build();
	}

	public static UEResult error(UERetCode ueRetCode){
		return error(ueRetCode.getCode(),ueRetCode.getMessage(),null);
	}


	public static UEResult success(){
		return UEResult.create().success()
				.msg(ApiConstant.CODE_MSG_SUCCESS)
				.build();
	}

	public static UEResult success(JSONObject data){
		return UEResult.create().success()
				.msg(ApiConstant.CODE_MSG_SUCCESS)
				.data(data)
				.build();
	}

	public static UEResult success(Map<String, Object> columns) {
		JSONObject jsonObject = new JSONObject(columns);
		return success(jsonObject);
	}

}
